package net.cyl.controller;


import net.cyl.controller.request.TrafficPageRequest;
import net.cyl.controller.request.UseTrafficRequest;
import net.cyl.service.TrafficService;
import net.cyl.util.JsonData;
import net.cyl.vo.TrafficVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 流量包信息表 前端控制器
 * </p>
 *
 * @author 曹玉林
 * @since 2025-05-09
 */
@RestController
@RequestMapping("/api/traffic/v1")
public class TrafficController {
    @Autowired
    private TrafficService trafficService;
    @Value("${rpc.token}")
    private String rpcToken;

    /**
     * 使用流量包API
     *
     * @param useTrafficRequest
     * @param request
     * @return
     */
    @PostMapping("reduce")
    public JsonData useTraffic(@RequestBody UseTrafficRequest useTrafficRequest, HttpServletRequest request){

        String requestToken = request.getHeader("rpc-token");
        if(rpcToken.equalsIgnoreCase(requestToken)){
            //具体使用流量包逻辑
            JsonData jsonData = trafficService.reduce(useTrafficRequest);
            return jsonData;
        }else {
            return JsonData.buildError("非法访问");
        }


    }
    @RequestMapping("/page")
    public JsonData pageAvailable(@RequestBody TrafficPageRequest request)  {
        Map<String, Object> map = trafficService.pageAvailable(request);
        return JsonData.buildSuccess(map);
    }

    /**
     * 查找某个流量包详情
     * @param trafficId
     * @return
     */
    @GetMapping("/detail/{trafficId}")
    public JsonData detail(@PathVariable("trafficId") long trafficId){

        TrafficVO trafficVO = trafficService.detail(trafficId);

        return JsonData.buildSuccess(trafficVO);
    }

}

